<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Base class for A rich text editor.
 * 
 * 用来显示一个内容输入器的控件.
 * MUST includes the 'kindeditor' js lib.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.1
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_widget.php';

// Definitions.
// The kindeditor paths.
define('KE_SKINSPATH',				slh_full_path('theme/base/kindeditor/skins/'));
define('KE_PLUGINSPATH',			slh_full_path('theme/base/kindeditor/plugins/'));
define('KE_UPLOADPHPSURL',			slh_full_path('extlib/kindeditor/upload_json.php'));
define('KE_FILEMANAGERPHPURL',		slh_full_path('extlib/kindeditor/file_manager_json.php'));

/**
 * Widget for displaying Editor.
 */
class Editor extends Widget
{
	/**
	 * The id of the widget.
	 */
	var $editorid;
	
	/**
	 * Width of the editor.
	 */
	var $width;
	
	/**
	 * Height of the editor.
	 */
	var $height;
	
	/**
	 * Constructor.
	 */
	function __construct($editorid, 
						 $out,
						 $width = 580,
						 $height = 400,
						 $args = array())
	{
		parent::__construct($out, $out->args);
		
		$this->editorid = $editorid;
		$this->width = $width;
		$this->height = $height;
	}
	
	/**
	 * Preparation for the initialization.
	 */
	function prepareData()
	{
		$this->out->hidden('ke_id', $this->editorid);
		$this->out->hidden('ke_skinspath', KE_SKINSPATH);
		$this->out->hidden('ke_pluginspath', KE_PLUGINSPATH); 
		$this->out->hidden('ke_uploadphpsurl', KE_UPLOADPHPSURL); 
		$this->out->hidden('ke_filemanagerphpurl', KE_FILEMANAGERPHPURL); 
	}
	
	/**
	 * Starts a editor.
	 * The notice or some sayings of the editor. Might some labels.
	 * 
	 * Might be overrided.!
	 */
	function showStart()
	{
		$this->out->elementStart('div', array('class' => 'cls_div_editor'));
	}
	
	/**
	 * Ends a editor.
	 */
	function showEnd()
	{
		$this->out->elementEnd('div');
	}
	
	/**
	 * Show the editor
	 */
	function show()
	{
		$this->prepareData();
		$this->showStart();
		
		$this->out->elementStart('textarea', array('id'	=> $this->editorid,
										   		   'name'=> $this->editorid,
												   'style'=> 'width:'.$this->width.'px;height:'.$this->height.'px;'));
		$rendered =& $this->getField($this->editorid);
		if ( !empty($rendered)) {
			try {
				$this->out->raw(slh_uncompress($rendered));
			} catch (Exception $e) {
				$this->out->raw($rendered);
			}
		}
		$this->out->elementEnd('textarea');
		
		$this->showEnd();
	}
}

?>